package cn.tedu.store.service.impl;

import cn.tedu.store.entity.Department;
import cn.tedu.store.entity.Employee;
import cn.tedu.store.entity.Meeting;
import cn.tedu.store.entity.MeetingCheckRecode;
import cn.tedu.store.entity.dto.MeetingCheckRecodeDto;
import cn.tedu.store.mapper.EmployeeMapper;
import cn.tedu.store.mapper.MeetingMapper;
import cn.tedu.store.service.IMeetingService;
import cn.tedu.store.temperatureDetection.entity.dto.EmployeeDto;
import cn.tedu.store.util.BaseQuery;
import cn.tedu.store.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MeetingServiceImpl implements IMeetingService {

    @Autowired
    private MeetingMapper meetingMapper;
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public void meetingApply(Meeting meeting) {

    }

    @Override
    public void insert_MeetingRecode(MeetingCheckRecode meetingCheckRecode) {

        String meetingCode = meetingMapper.meetingCode();
        //System.out.println(meetingCode);

        //会议号
        meetingCheckRecode.setMeetingCode(meetingCode);
        //会议有效性
        meetingCheckRecode.setEmpMeetingvalid("1");
        //会议开始签到时间
        meetingCheckRecode.setEmpBeginDate(new Date());

        //插入会议签到记录
        Integer row = meetingMapper.insert_MeetingRecode(meetingCheckRecode);

//
//
//        //通过扫码电话获取申请人
//        List<Employee> employees = meetingMapper.selectEmployee();
//        String employeeName = employees.get(0).getEmployeeName();
//        //会议签到人工号
//        meetingCheckRecode.setEmpCode();


    }

    @Override
    public String meetingCode() {
        return null;
    }

    @Override
    public List<Employee> selectEmployee(String phone) {
        return meetingMapper.selectEmployee(phone);
    }

    @Override
    public List<Department> selectDepartment() {
        return meetingMapper.selectDepartment();
    }

    @Override
    public MeetingCheckRecodeDto signMeeting(MeetingCheckRecodeDto meetingCheckRecodeDto) {
        return null;
    }

    @Override
    public PageList<EmployeeDto> selectEmployeeName(String meetingCode, BaseQuery baseQuery) {
        //创建一个分页的对象
        PageList<EmployeeDto> pageList = new PageList<>();
        //Meeting bymeetingCode = meetingMapper.findBymeetingCode(meetingCheckRecode.getMeetingCode());
        //根据会议编号查询已经签到的员工信息
        List<EmployeeDto> employees = employeeMapper.findLateEmployeeByMeetingCode(meetingCode);
        if (employees.isEmpty())
            return null;
            //throw new RuntimeException("暂无签到的员工");
        //根据签到时间来获取签到正常的员工姓名getMBeginDate
        //List<EmployeeDto> employeeList = employees.stream().filter(employee ->
        //                employee.getPBeginDate().before(employee.getMBeginDate())).
        //        collect(Collectors.toList());
        //以流的形式进行分页查询
        List<EmployeeDto> collect = employees.stream().
                skip((baseQuery.getPage() - 1) * baseQuery.getPageSize()).
                limit(baseQuery.getPageSize()).collect(Collectors.toList());
        pageList.setTotal(collect.size());
        pageList.setList(collect);
        return pageList;
    }

    @Override
    public PageList<EmployeeDto> selectLateEmployee(String meetingCode, BaseQuery baseQuery) {
        //创建一个分页的对象
        PageList<EmployeeDto> pageList = new PageList<>();
        List<EmployeeDto> employees = employeeMapper.findEmployeeByMeetingCode(meetingCode);
        if (employees.isEmpty()){
            return null;
            //throw new RuntimeException("暂无迟到的人员");

        }
        //List<EmployeeDto> employeeList = employees.stream().filter(employee ->
        //                employee.getMBeginDate().after(employee.getPBeginDate())).
        //        collect(Collectors.toList());
        //以流的形式进行分页查询
        List<EmployeeDto> collect = employees.stream().
                skip((baseQuery.getPage() - 1) * baseQuery.getPageSize()).
                limit(baseQuery.getPageSize()).collect(Collectors.toList());
        pageList.setTotal(collect.size());
        pageList.setList(collect);
        return pageList;
    }

    @Override
    public PageList<EmployeeDto> selectNotSingInEmployee(String meetingCode, BaseQuery baseQuery) {
        PageList<EmployeeDto> pageList = new PageList<>();
        List<EmployeeDto> all = employeeMapper.findAll();
        List<EmployeeDto> employees = employeeMapper.findEmployeeByMeetingCode(meetingCode);
        List<EmployeeDto> employeeList = employeeMapper.findEmployeeByMeetingCode(meetingCode);
        employees.addAll(employeeList);
        HashSet<EmployeeDto> h1 = new HashSet<>(all);
        HashSet<EmployeeDto> h2 = new HashSet<>(employeeList);
        h1.remove(h2);
        List<EmployeeDto> list = new ArrayList<>();
        list.addAll(h1);
        List<EmployeeDto> collect = list.stream().
                skip((baseQuery.getPage() - 1) * baseQuery.getPageSize()).
                limit(baseQuery.getPageSize()).collect(Collectors.toList());
        pageList.setTotal(collect.size());
        pageList.setList(list);
        return pageList;
    }

    @Override
    public Meeting getMeetingName(String mCode) {
        return meetingMapper.findBymeetingCode(mCode);
    }


    //新建会议
    @Override
    public void insertMeeting(Meeting meeting) {
        meeting.setMeetingValidity("1");
        meeting.setMeetingBegindate(new Date());
        meetingMapper.insertMeeting(meeting);
    }



}
